﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace DAL
{
    public class ImageDAL
    {
        public ImageDAL() { }
        #region[Image_SelectByID]
        public DTO.ImageDTO Image_SelectByID(int id)
        {
            DTO.ImageDTO img = new DTO.ImageDTO();
            Connection connecttion = new Connection();
            MySqlConnection con = new MySqlConnection(connecttion.Connect);
            MySqlCommand cmd = new MySqlCommand("SelectImageByID", con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@imageid", id);
            con.Open();
            MySqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                img.ImageID = dr.GetInt32(0);
                img.Image = (byte[])(dr["Image"]);
            }
            dr.Close();
            con.Close();
            return img;
        }
        #endregion

        #region[Image_SelectByName]
        public int Image_SelectByName(byte[] image)
        {
            int id = 0;
            Connection connecttion = new Connection();
            MySqlConnection con = new MySqlConnection(connecttion.Connect);
            MySqlCommand cmd = new MySqlCommand("SelectImageByValue", con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@image", image);
            con.Open();
            MySqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                id = dr.GetInt32(0);
              
            }
            dr.Close();
            con.Close();
            return id;
        }
        #endregion

        #region [Image_Insert]
        public bool Image_Insert(DTO.ImageDTO img)
        {
            Connection connecttion = new Connection();
            MySqlConnection con = new MySqlConnection(connecttion.Connect);
            MySqlCommand cmd = new MySqlCommand("AddImage", con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            con.Open();
            cmd.Parameters.Add(new MySqlParameter("@image", img.Image));
            cmd.ExecuteNonQuery();


            con.Close();

            return true;
        }
        #endregion

        #region [Image_Delete]
        public bool Image_DeleteByID(DTO.ImageDTO img)
        {
            Connection connecttion = new Connection();
            MySqlConnection con = new MySqlConnection(connecttion.Connect);
            MySqlCommand cmd = new MySqlCommand("DeleteImageByID", con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;

            cmd.Parameters.Add(new MySqlParameter("@imageid", img.ImageID));

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            return true;
        }
        #endregion

    }
}
